﻿using System;
using System.ServiceModel;
using System.Web.Security;
using System.Data;

namespace WcfService
{
    public partial interface IService
    {
        [OperationContract]
        string[] aspnet_Roles_GetAllRoles();

        [OperationContract]
        DataTable aspnet_Roles_GetAll();

        [OperationContract]
        string[] aspnet_Roles_GetRolesForUser(string UserName);

        [OperationContract]
        string[] aspnet_Roles_GetUsersInRole(string roleName);

        [OperationContract]
        string aspnet_Roles_CreateRole(string roleName);

        [OperationContract]
        bool aspnet_Roles_Update(string roleID, string roleName);

        [OperationContract]
        string aspnet_Roles_DeleteRole(string roleName);

        [OperationContract]
        string aspnet_Roles_AddUserToRole(string userName, string roleName);

        [OperationContract]
        string aspnet_Roles_RemoveUserFromRole(string userName, string roleName);

        [OperationContract]
        bool aspnet_Roles_IsUserInRole(string userName, string roleName);

        [OperationContract]
        bool aspnet_Roles_RoleExists(string roleName);

    }

    public partial class Service : IService
    {

        public string[] aspnet_Roles_GetAllRoles()
        {
            return Roles.GetAllRoles();
        }

        public DataTable aspnet_Roles_GetAll()
        {
            return SqlHelper.ExecuteDataset(Properties.Settings.Default.ConnectionString, "aspnet_Roles_GetALL").Tables[0];
        }

        public string[] aspnet_Roles_GetRolesForUser(string UserName)
        {
            return Roles.GetRolesForUser(UserName);
        }

        public string[] aspnet_Roles_GetUsersInRole(string roleName)
        {
            return Roles.GetUsersInRole(roleName);
        }

        public string aspnet_Roles_CreateRole(string roleName)
        {
            string retVal = "";
            try
            {
                Roles.CreateRole(roleName);
                retVal = "true";
            }
            catch (Exception ex)
            {
                retVal = ex.Message;
            }
            return retVal;
        }

        public string aspnet_Roles_DeleteRole(string roleName)
        {
            string retVal = "false";
            try
            {
                if (Roles.DeleteRole(roleName))
                    retVal = "true";
            }
            catch (Exception ex)
            {
                retVal = ex.Message;
            }
            return retVal;
        }

        public bool aspnet_Roles_Update(string roleID, string roleName)
        {
            return (SqlHelper.ExecuteNonQuery(Properties.Settings.Default.ConnectionString, "aspnet_Roles_Update", roleID, roleName) > 0);
        }

        public string aspnet_Roles_AddUserToRole(string userName, string roleName)
        {
            string retVal = "";
            try
            {
                Roles.AddUserToRole(userName, roleName);
                retVal = "true";
            }
            catch (Exception ex)
            {
                retVal = ex.Message;
            }

            return retVal;
        }

        public string aspnet_Roles_RemoveUserFromRole(string userName, string roleName)
        {
            string retVal = "";
            try
            {
                Roles.RemoveUserFromRole(userName, roleName);
                retVal = "true";
            }
            catch (Exception ex)
            {
                retVal = ex.Message;
            }

            return retVal;
        }

        public bool aspnet_Roles_IsUserInRole(string userName, string roleName)
        {
            return Roles.IsUserInRole(userName, roleName);
        }

        public bool aspnet_Roles_RoleExists(string roleName)
        {
            return Roles.RoleExists(roleName);
        }

    }


}
